package com.bailan.foreat.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 基于死信队列,配置延迟处理
 */
@Configuration
public class DelayCancelOrderConfig {

    //死信部分
    @Bean
    public FanoutExchange cancelOrderDeadLetterExchange(){
        return new FanoutExchange("cancelOrderDeadLetterExchange");
    }

    @Bean
    public Queue cancelOrderDeadLetterQueue(){
        return new Queue("cancelOrderDeadLetterQueue");
    }

    @Bean
    public Binding cancelOrderDeadLetterBinding(){
        return BindingBuilder.bind(cancelOrderDeadLetterQueue()).to(cancelOrderDeadLetterExchange());
    }


    //normal部分
    @Bean
    public DirectExchange cancelOrderNormalExchange(){
        return new DirectExchange("cancelOrderNormalExchange");
    }

    //正常队列不能有消费者
    @Bean
    public Queue cancelOrderNormalQueue(){
        return QueueBuilder.durable("cancelOrderNormalQueue")
                .deadLetterExchange(cancelOrderDeadLetterExchange().getName())
                .deadLetterRoutingKey("xxxxx")  //因为死信部分交换机是Fanout广播类型,所以这里routingKey随便写一个就行
                .ttl(30*1000)  //指定ttl
                .build();
    }

    @Bean
    public Binding cancelOrderNormalBinding(){
        return BindingBuilder.bind(cancelOrderNormalQueue())
                .to(cancelOrderNormalExchange())
                .with("delayCancelOrder");
    }



    //死信部分
    @Bean
    public FanoutExchange finishOrderDeadLetterExchange(){
        return new FanoutExchange("finishOrderDeadLetterExchange");
    }
    @Bean
    public Queue finishOrderDeadLetterQueue(){
        return new Queue("finishOrderDeadLetterQueue");
    }
    @Bean
    public Binding finishOrderDeadLetterBinding(){
        return BindingBuilder.bind(finishOrderDeadLetterQueue()).to(finishOrderDeadLetterExchange());
    }
    //normal部分
    @Bean
    public DirectExchange finishOrderNormalExchange(){
        return new DirectExchange("finishOrderNormalExchange");
    }
    //正常队列不能有消费者
    @Bean
    public Queue finishOrderNormalQueue(){
        return QueueBuilder.durable("finishOrderNormalQueue")
                .deadLetterExchange(finishOrderDeadLetterExchange().getName())
                .deadLetterRoutingKey("xxxxx")  //因为死信部分交换机是Fanout广播类型,所以这里routingKey随便写一个就行
                .ttl(30*1000)  //指定ttl
                .build();
    }
    @Bean
    public Binding finishOrderNormalBinding(){
        return BindingBuilder.bind(finishOrderNormalQueue())
                .to(finishOrderNormalExchange())
                .with("delayFinishOrder");
    }
}
